import 'package:flutter/material.dart';
import 'package:flutter1/demo/app_lifecycle.dart';
import 'package:flutter1/demo/gesture_page.dart';
import 'package:flutter1/demo/layout_page.dart';
import 'package:flutter1/demo/less_group_page.dart';
import 'package:flutter1/demo/res_page.dart';
import 'package:flutter1/demo/statefall_group_page.dart';
import 'package:flutter1/main.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';

import 'MainPage.dart';
import 'launch_page.dart';
import 'lifecycle_page.dart';
import 'photo_app.dart';
import 'theme_page.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        fontFamily: "jcjdht",
        colorScheme:
            ColorScheme.fromSeed(seedColor: ColorUtil.color('#f2f2f2')),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
            title: Text('路由'),
            backgroundColor: Theme.of(context).colorScheme.inversePrimary),
        body: RouteNevigator(),
      ),

      routes: <String, WidgetBuilder>{
        '/layout': (BuildContext context) => const LayoutPage(),
        '/less': (BuildContext context) => const LessGroupPage(),
        '/ful': (BuildContext context) => const StatefallGroupPage(),
        '/gesture': (BuildContext context) => const GesturePage(),
        '/res': (BuildContext context) => const ResPage(),
        '/launch': (BuildContext context) => const LaunchPage(),
        '/applifecycle': (BuildContext context) => const AppLifecycle(),
        '/theme': (BuildContext context) => const ThemePage(),
        '/photo': (BuildContext context) => const PhotoApp(),
        '/main': (BuildContext context) => const MainPage(),
      },
    );
  }
}

class RouteNevigator extends StatefulWidget {
  @override
  _RouteNevigatorState createState() => _RouteNevigatorState();
}

class _RouteNevigatorState extends State<RouteNevigator> {
  bool byName = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Text(
            '我是牛逼的字体',
            style: TextStyle(fontFamily: "china", fontSize: 45),
          ),
          SwitchListTile(
              title: Text('${byName ? '' : '不'}通过路由跳转'),
              value: byName,
              onChanged: (value) {
                setState(() {
                  byName = value;
                });
              }),
          _item('布局', LayoutPage(), "layout"),
          _item('无状态', LessGroupPage(), "less"),
          _item('有状态', StatefallGroupPage(), "ful"),
          _item('手势', GesturePage(), "gesture"),
          _item('资源', ResPage(), "res"),
          _item('三方应用', LaunchPage(), "launch"),
          _item('生命周期', LifecyclePage(), "lifecycle"),
          _item('应用生命周期', AppLifecycle(), "applifecycle"),
          _item('修改主题', ThemePage(), "theme"),
          _item('拍照', PhotoApp(), "photo"),
          _item('进入主页', MainPage(), "main"),
        ],
      ),
    );
  }

  _item(String title, page, String routeName) {
    return Container(
      child: TextButton(
          onPressed: () {
            if (byName) {
              Navigator.pushNamed(context, routeName);
            } else {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => page));
            }
          },
          child: Text(title)),
    );
  }
}
